<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use app\models\{Order, Wechat, Payment, Coupon,User as Users};
use think\Db;
use think\facade\Log;
use app\facade\Redis;

class Refund extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Refund')->setDescription('订单退款');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        (new Order())->where(['status' => [2, 3], 'shouhou' => 2, 'tuikuan' => 2])->chunk(1, function ($lists) {
            foreach ($lists as $list) {
                $frtimes = Redis::get('orderRefund' . $list['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('orderRefund' . $list['id'], 10, 10);
                }
                $payment = (new Payment())->where(['ordersn' => $list['ordersn'], 'status' => 2, 'type' => 1])->find();
                if($list['payment'] == 3 ){
                    //余额退款
                    Db::startTrans();
                    try {
                        (new Order())->where(['status' => $list['status'], 'id' => $list['id']])->update(['shouhou' => 1, 'status' => 5, 'caceltimes' => date('Y-m-d H:i:s')]);
                        if ($list['coupon'] > 0) {
                            (new Coupon())->where('id', $list['coupon'])->update(['status' => 1]);
                        }
                        if ($list['total'] > 0) {
                            $user = (new Users())->where(['id' => $list['user_id']])->find();
                            (new Users())->MoneyEdit($user['id'], $user['money'], $list['total'], 3, $list['id'], '购买商品取消');
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        echo $e->getMessage();
                        continue;
                    }
                }elseif (empty($payment) || $list['payment'] == 0 || $payment['refund'] == 2) {
                    Db::startTrans();
                    try {
                        (new Order())->where(['status' => $list['status'], 'id' => $list['id']])->update(['shouhou' => 1, 'status' => 5, 'caceltimes' => date('Y-m-d H:i:s')]);
                        if ($list['coupon'] > 0) {
                            (new Coupon())->where('id', $list['coupon'])->update(['status' => 1]);
                        }
                        Db::commit();
                    } catch (\Exception $e) {
                        Db::rollback();
                        echo $e->getMessage();
                        continue;
                    }
                } else {
                    switch ($payment['paytype']) {
                            //微信支付退款
                        case '1':
                            $refundInfo['refund_id'] = $payment['id'] . time();
                            $refundInfo['money'] = $payment['money'];
                            $paymentInfo['params'] = $payment['paydata'];
                            $paymentInfo['payment_id'] =  $payment['paymentid'];
                            $paymentInfo['money'] = $payment['money'];
                            $res = (new Wechat())->refund($refundInfo, $paymentInfo);
                            if ($res['status'] == 'false') {
                                echo $res['msg'];
                                continue;
                            }
                            dump($res);
                            break;
                            break;
                        default:
                            # code...
                            break;
                    }
                }
            }
        });
    }
}
